fn main() {
    test1_tradition_get_firstname();
    println!("---------------------");
    test2_slice();
    println!("---------------------");
    test3_slice_get_firstword();
    println!("---------------------");
    test4_other_slice();
}
fn test1_tradition_get_firstname() {
    let s = String::from("hello");
    let index = first_word(&s);
    println!("The index of first word is : {index}");
}
fn first_word(str: &String) -> usize {
    let byte_arr = str.as_bytes();
    for (i, &item) in byte_arr.iter().enumerate() {
        if item == b' ' {
            return i;
        }
    }
    str.len()
}
fn test2_slice() {
    let s = String::from("hello world");
    let hello = &s[0..5];
    let world = &s[6..11];

    println!("{hello},{world}");

    let len = s.len();
    let slice = &s[3..len];
    println!("{slice}");
    let slice = &s[3..];
    println!("{slice}");
    let slice = &s[0..len];
    println!("{slice}");
    let slice = &s[..];
    println!("{slice}");
}
fn test3_slice_get_firstword() {
    let mut s = String::from("hello world");
    let word = first_word_with_slice(&s);
    //s.clear();//err
    println!("The index of first word is : {}", word.len());
}
//fn first_word_with_slice(str: &String) -> &str {
fn first_word_with_slice(str: &str) -> &str {
    //more better
    let byte_arr = str.as_bytes();
    for (i, &item) in byte_arr.iter().enumerate() {
        if item == b' ' {
            return &str[..i];
        }
    }
    &str[..]
}
fn test4_other_slice() {
    let a = [1, 2, 3, 4, 5];
    let slice = &a[1..3];
    assert_eq!(slice, &[2, 3]);
}
